using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Frogger_Game
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class FrameRateCounter : DrawableGameComponent
    {
        ContentManager content;
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;

        int frameRate = 0;
        int frameCounter = 0;
        TimeSpan elapsedTime = TimeSpan.Zero;


        public FrameRateCounter(Game game)
            : base(game)
        {
            content = new ContentManager(game.Services);
        }

        public override void Initialize()
        {
            // TODO: Add your initialization logic here  

            base.Initialize();
        }


        protected override void LoadContent()
        {

                spriteBatch = new SpriteBatch(GraphicsDevice);
                spriteFont = Game.Content.Load<SpriteFont>("gamefont");

        }


        protected override void Dispose(bool unloadAllContent)
        {
            if (unloadAllContent)
                Game.Content.Unload();
        }


        public override void Update(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime > TimeSpan.FromSeconds(1))
            {
                elapsedTime -= TimeSpan.FromSeconds(1);
                frameRate = frameCounter;
                frameCounter = 0;
            }

        }


        public override void Draw(GameTime gameTime)
        {
            frameCounter++;

            string fps = string.Format("fps: {0}", frameRate);
            //string life = string.Format("Life: " + GameState.game_state.Life);
            spriteBatch.Begin();

            spriteBatch.DrawString(spriteFont, fps, new Vector2(12, 12), Color.Black);
            spriteBatch.DrawString(spriteFont, fps, new Vector2(11, 11), Color.White);
            //spriteBatch.DrawString(spriteFont, life, new Vector2(12, 701), Color.Black);
            //spriteBatch.DrawString(spriteFont, life, new Vector2(11, 700), Color.White);

            //float ds = (float)gameTime.ElapsedGameTime.TotalSeconds;

            //System.Console.WriteLine("ds" + ds + " dt " + dt)

            //if (WinLogic.time_finished == false)
            //{
                //spriteBatch.Draw(back, new Rectangle(GraphicsDevice.Viewport.Width * 3 / 4, GraphicsDevice.Viewport.Height * 11 / 12, (int)WinLogic.time_limit * 4, 20), new Rectangle(800, 700, (int)WinLogic.time_limit * 4, 20), Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
                //if (GameState.game_state.time_counter > WinLogic.time_limit - WinLogic.time_limit / 4)
                //{
                //    spriteBatch.Draw(back, new Rectangle(GraphicsDevice.Viewport.Width * 3 / 4, GraphicsDevice.Viewport.Height * 11 / 12, (int)WinLogic.time_limit * 4 - (int)GameState.game_state.time_counter * 4, 20), new Rectangle(800, 700, (int)WinLogic.time_limit * 4 - (int)GameState.game_state.time_counter * 4, 20), Color.Red, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
                //}
                //else
                //{
                //    spriteBatch.Draw(back, new Rectangle(GraphicsDevice.Viewport.Width * 3 / 4, GraphicsDevice.Viewport.Height * 11 / 12, (int)WinLogic.time_limit * 4 - (int)GameState.game_state.time_counter * 4, 20), new Rectangle(800, 700, (int)WinLogic.time_limit * 4 - (int)GameState.game_state.time_counter * 4, 20), Color.Green, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
                //}

            //}

           // spriteBatch.DrawString(spriteFont, "LEVEL: " + GameState.game_state.level, new Vector2(GraphicsDevice.Viewport.Width - 200, 20), Color.White);

           // if (WinLogic.win_level == true)
           // {
           //         spriteBatch.DrawString(spriteFont, "VINTO, FINITO LIVELLO!", new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2), Color.Red);
               
           // }
           // if (WinLogic.lost_level == true)
           // {
           //     spriteBatch.DrawString(spriteFont, "GAME OVER!", new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2), Color.Red);
           //}

            //MODO 1:
            //var delta = 50 - ds;


            //if (delta > 0)
            //{
            //    spriteBatch.Draw(back, new Rectangle(800, 700, 50, 20), new Rectangle(800, 700, 50, 20), Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
            //    spriteBatch.Draw(back, new Rectangle(800, 700, (int)delta, 20), new Rectangle(800, 700, (int)delta, 20), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
            //}



            //MODO 2:
            //float ds = (float)gameTime.ElapsedGameTime.TotalSeconds;
            //sec_par = sec_par + ds;
            //if (sec_par < 100)
            //{
                //spriteBatch.Draw(back, new Rectangle(800, 700, 100, 20), new Rectangle(800, 700, 200, 20), Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
                //spriteBatch.Draw(back, new Rectangle(800, 700, 100 - (int)sec_par, 20), new Rectangle(800, 700, 100 - (int)sec_par, 20), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
            //}

            //if (timer.isActive == true)
            //{

            //    spriteBatch.Draw(back, new Rectangle(800, 700, 200, 20), new Rectangle(800, 700, 200, 20), Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
            //    spriteBatch.Draw(back, new Rectangle(800, 700, timer.displayInt * 2, 20), new Rectangle(800, 700, timer.displayInt * 2, 20), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
                
            //    //spriteBatch.DrawString(spriteFont, timer.displayValue, new Vector2(900, 701), Color.Black);
            //    //spriteBatch.DrawString(spriteFont, timer.displayValue, new Vector2(899, 700), Color.White);
            //}


            spriteBatch.End();

            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        }
    }
}
